/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulo1.cgd;

import java.util.List;
import javax.persistence.Query;
import modulo1.cdp.Aluno;
import modulo1.cdp.EnumEstadoTurma;
import modulo1.cdp.Matricula;
import modulo1.cdp.Turma;

/**
 *
 * @author ifes
 */
public class MatriculaDaoJpa extends DaoJpa<Matricula> {

    public List<Matricula> buscar() throws Exception {
        try {
            Query q = em.createQuery("SELECT m FROM Matricula m");
            return q.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Matricula> buscarAno(int ano) throws Exception {
        try {
            Query q = em.createQuery("SELECT m FROM Matricula m WHERE m.turma.ano = " + ano + " ORDER BY m.turma.periodo DESC");
            return q.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Matricula consultar(Turma turma, Aluno aluno) throws Exception {
        try {
            Query q = em.createQuery(""
                    + "SELECT m FROM Matricula m "
                    + "WHERE "
                    + "     m.turma.id = " + turma.getId() + " "
                    + "AND "
                    + "     m.aluno.id = " + aluno.getId());
            return (Matricula) q.getResultList().get(0);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Matricula> consultar(Aluno aluno) throws Exception {
        try {
            Query q = em.createQuery(""
                    + "SELECT m FROM Matricula m "
                    + "WHERE "                    
                    + "     m.aluno.id = " + aluno.getId()
                    + " AND "
                    + "     m.turma.estado = :estado "
                    + " ORDER BY m.turma.disciplina.periodo");
            q.setParameter("estado", EnumEstadoTurma.ENCERRADA);
            return q.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }
}
